﻿@using ViewModels

@model UserInfo


<ul class="form-group" id="messagesListUl" style="margin-bottom:20px">
    <li>Message List</li>
</ul>

<form>
    <div class="form-group">
        <label for="">当前登录者--姓名:@Model.UserName</label>
        <label for="">当前登录者--Id:@Model.UserId</label>
    </div>
    <div class="form-group">
        <label for="username">向:</label>
        <input type="text" class="form-control" id="username" name="username">
    </div>
    <div class="form-group">
        <label for="msgcontent">发送消息内容:</label>
        <textarea rows="5" cols="20" id="msgcontent" name="msgcontent" class="form-control"></textarea>
    </div>

    <button type="button" onclick="btnSend2Msg()">一对一消息</button>
</form>


<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/signalr/dist/browser/signalr.js"></script>
<script src="~/js/mySignalR.js"></script>
<script type="text/javascript">


    //发送一对一消息
    function btnSend2Msg(){
        $.ajax({
            url:'/home/PostData',
            type:'post',
            data:{userId:$('#username').val(),message:$('#msgcontent').val()},
            dataType:'json',
            async:true,
            success:function(data){
               if(data.code=='success'){
                    alert(data.msg);
                }
                else{
                    alert("发送失败");
                }
            }
        });
    }

    //绑定事件("ReceiveMessage"和服务器端的SendPrivateMessage方法中的第一个参数一致)
    signalr_connection.on("ReceivePrivateMessage", function(data) {

        console.info('msg:',data)

     
        const li = document.createElement("li");
        li.textContent =  data.message;
        document.getElementById("messagesListUl").appendChild(li);
    });



</script>